package com.tanhua.mongo.api;

import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询动态评论列表
     *
     * @param page
     * @param pagesize
     * @param movementId
     * @return
     */
    @Override
    public List<Comment> comments(int page, int pagesize, String movementId,CommentType commentType) {

        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(commentType.getType()))
                .skip((page - 1 ) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));

        return mongoTemplate.find(query, Comment.class);
    }

    /**
     * 保存评论
     *
     * @param comment
     */
    @Override
    public Integer saveComments(Comment comment) {
        //查询评论的动态获取动态的发布人id
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);

        if (movement != null){
            comment.setPublishUserId(movement.getUserId());
        }
        //保存评论信息
        mongoTemplate.save(comment);
        //在动态表中更新对应的字段总数
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()){
            update.inc("likeCount",1);
        }else if (comment.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount",1);
        }else {
            update.inc("loveCount",1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        //更新完获取更新完的最新信息
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(query, update,options , Movement.class);

        //获取最新的评论数量，并返回
        return modify.statisCount(comment.getCommentType());

    }

    /**
     * 判断当前评论是否点赞过
     *
     * @param movementId
     * @param commentType
     * @param userId
     * @return
     */
    @Override
    public boolean findComment(String movementId, CommentType commentType, Long userId) {

        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType.getType())
                .and("userId").is(userId));

        return mongoTemplate.exists(query, Comment.class);
    }

    /**
     * 取消点赞或者喜欢
     *
     * @param comment
     * @return
     */
    @Override
    public Integer delete(Comment comment) {
        //删除comment表中的数据
        Query query = Query.query(Criteria.where("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType())
                .and("userId").is(comment.getUserId()));
        //删除评论点赞或者喜欢
        mongoTemplate.remove(query,Comment.class);
        //在动态表中更新对应的字段总数
        Query query1 = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()){
            update.inc("likeCount",-1);
        }else if (comment.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        //更新完获取更新完的最新信息
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(query1, update,options, Movement.class);

        //获取最新的数量，并返回
        return modify.statisCount(comment.getCommentType());
    }

    /**
     * 评论点赞
     * @param commentId
     * @return
     */
    @Override
    public Integer saveLikeCount(String commentId) {
        Query query = Query.query(Criteria.where("id").is(new ObjectId(commentId)));

        Update update = new Update();
        update.inc("likeCount",1);
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        //更新完获取更新完的最新信息
        options.returnNew(true);
        Comment modify = mongoTemplate.findAndModify(query, update, options, Comment.class);

        return modify.getLikeCount();
    }

    /**
     * 评论取消点赞
     *
     * @param commentId
     * @return
     */
    @Override
    public Integer dislike(String commentId) {
        Query query = Query.query(Criteria.where("id").is(new ObjectId(commentId)));

        Update update = new Update();
        update.inc("likeCount",-1);
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        //更新完获取更新完的最新信息
        options.returnNew(true);
        Comment modify = mongoTemplate.findAndModify(query, update, options, Comment.class);

        return modify.getLikeCount();
    }

    /**
     * 获取点赞人或者评论人的id
     *
     * @param page
     * @param pagesize
     * @param commentType
     * @param userId
     * @return
     */
    @Override
    public List<Comment> findLikes(int page, int pagesize, CommentType commentType, Long userId) {
        Query query = Query.query(Criteria.where("commentType").is(commentType.getType()).and("publishUserId").is(userId))
                .skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query,Comment.class);
    }

}
